'use strict';

const { Controller } = require('egg');

class SchoolController extends Controller {
  async index() {
    const { ctx } = this;
    let { page, pagesize, educationType } = ctx.request.query;
    page = page ? parseInt(page) : 1;
    pagesize = pagesize ? parseInt(pagesize) : 0;
    const offset = (page - 1) * pagesize;
    const option = {};
    if (educationType) {
      option.educationType = educationType;
    }
    const result = await ctx.service.school.getList(offset, pagesize, option);
    const count = await ctx.service.school.getCount(option);
    ctx.returnBody({
      data: result,
      count,
    });
  }

  async create() {
    const { ctx } = this;
    const school = ctx.request.body;
    const result = await ctx.service.school.create(school);
    if (result.affectedRows) {
      ctx.returnBody({
        message: '创建成功',
      });
    } else {
      ctx.returnBody({
        code: 204,
        data: result,
        message: '创建失败',
      });
    }
  }
}

module.exports = SchoolController;
